﻿#ifndef ANCHORITEM_H
#define ANCHORITEM_H
#include <QObject>
#include <QGraphicsItem>
#include <QTransform>
#include <QPainter>
#include <QCursor>
#include <QKeyEvent>
#include <QGraphicsSceneHoverEvent>
#include <QGraphicsSceneContextMenuEvent>
#include <QStyleOptionGraphicsItem>
#include <QTimer>
#include <QPixmap>

class AnchorItem :public QObject, public QGraphicsItem
{
	Q_OBJECT
public:
	AnchorItem(QRectF rect = QRectF(0, 0, 200, 200), QPixmap map = QPixmap(":/icons/circle_green.png"));
	QRectF boundingRect() const override;
protected:
	void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) override;
	void keyPressEvent(QKeyEvent* event) override;
	void mousePressEvent(QGraphicsSceneMouseEvent* event) override;
	void mouseMoveEvent(QGraphicsSceneMouseEvent* event) override;
	void mouseReleaseEvent(QGraphicsSceneMouseEvent* event) override;
	void hoverEnterEvent(QGraphicsSceneHoverEvent* event) override;
	void hoverMoveEvent(QGraphicsSceneHoverEvent* event) override;
	void hoverLeaveEvent(QGraphicsSceneHoverEvent* event) override;
	void contextMenuEvent(QGraphicsSceneContextMenuEvent* event) override;
	QVariant itemChange(GraphicsItemChange change, const QVariant& value) override;
private:
	// 内容区域大小
	QRectF getItemRect() const;
	void InitPixmap();
	// 调整控制点的位置
	void ItemResize();
	Qt::CursorShape GetResizeCursorShape(qreal angle);
signals:
	void itemPos(QPointF pos);
private:
	QColor brushColor;
	// 图片的宽高比
	qreal m_ItemRatio;
	// 外边框到内部图片之间的距离
	int m_Internal = 15;
	// 4个缩放角点的大小
	int m_EllipseWidth = 30;
	// 旋转控制点到图元的距离
	int m_LineLen = 30;
	// 旋转控制点大小
	int m_RotateEllipseWidth = 20;

	QPixmap m_RotatePixmap;
	QPixmap m_ScalePixmap;
	QPixmap m_ItemPixmap;
	QSizeF m_ItemSize;
	QTransform m_Transform;

	enum ItemOperation {
		ITEM_NONE = 0x00,
		ITEM_MOVE = 0x01,
		ITEM_RESIZE = 0x02,
		ITEM_ROTATE = 0x03
	};

	enum CursorType {
		ARROR_CURSOR = 0x00,
		RESIZE_TOPLEFT_CURSOR = 0x01,
		RESIZE_TOPRIGHT_CURSOR,
		RESIZE_BOTTOMLEFT_CURSOR,
		RESIZE_BOTTOMRIGHT_CURSOR,
		HAND_OPEN_CURSOR,
		HAND_CLOSE_CURSOR,
		ROTATE_CURSOR,
	};

	int m_Operation;
	int m_CursorShape;
	QRectF m_TopLeftRect, m_TopRightRect, m_BottomLeftRect, m_BottomRightRect, m_RotateRect;
	qreal m_TopLeftAngle, m_TopRightAngle, m_BottomLeftAngle, m_BottomRightAngle, m_RotateAngle;
	QCursor m_RotateHoverCursor;
	QCursor m_RotatePressCursor;

	QPointF m_CurrentScenePos, m_PressScenePos;
	QPointF m_PressItemPos;
};

#endif // ANCHORITEM_H
